home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / Frontier 4.0.1 / Frontier SDK 4.0b2 / Sample Code / Minimal Applet / minapp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-25  |  11.7 KB  |  660 lines  |  [TEXT/CWIE]

  1.  
  2. /*© copyright 1991-96 UserLand Software, Inc. All Rights Reserved.*/
  3.  
  4.  
  5. #include <applet.h>
  6. #include <applettextedit.h>
  7.  
  8.  
  9.  
  10.  
  11. #define uppercasetoken    'ucas'
  12. #define lowercasetoken    'lcas'
  13.  
  14. #define fastinserttoken    'fins'
  15.  
  16.  
  17. typedef struct tydata { /*what app.appdata points to*/
  18.     
  19.     boolean flactive;
  20.     
  21.     hdleditrecord heditrecord;
  22.     
  23.     PicHandle macpicture; /*used for printing*/
  24.     } tydata, **hdldata;
  25.     
  26.     
  27. typedef struct tyoptionsrecord { /*information we save in every data file*/
  28.     
  29.     short selstart, selend;
  30.     
  31.     char waste [64];
  32.     } tyoptionsrecord, **hdloptionsrecord;
  33.     
  34.     
  35.     
  36. #define wordwrapping true /*if false, it's a script editor; true, text editor*/
  37.  
  38.  
  39.  
  40.     
  41. static void madechanges (void) {
  42.     
  43.     hdlappwindow ha = app.appwindow;
  44.     
  45.     if (ha != nil) {
  46.     
  47.         (**ha).flmadechanges = true;
  48.     
  49.         /*(**ha).flresetscrollbars = true;*/
  50.         }
  51.     } /*madechanges*/
  52.     
  53.     
  54. static boolean newrecord (void) {
  55.     
  56.     hdlappwindow ha = app.appwindow;
  57.     hdldata hdata = nil;
  58.     hdleditrecord heditrecord = nil;
  59.     bigstring bs;
  60.     boolean fl;
  61.     
  62.     if (!newclearhandle (longsizeof (tydata), (Handle *) &hdata))
  63.         return (false);
  64.     
  65.     setstringlength (bs, 0);
  66.     
  67.     pushstyle ((**ha).defaultfont, (**ha).defaultsize, (**ha).defaultstyle);
  68.     
  69.     fl = editnewbuffer ((**ha).contentrect, wordwrapping, bs, &heditrecord);
  70.         
  71.     popstyle ();
  72.     
  73.     if (!fl)
  74.         goto error;
  75.         
  76.     (**heditrecord).flwindowbased = true;
  77.         
  78.     editautoscroll (heditrecord); /*automatically scroll for drags across bottom of window*/
  79.         
  80.     (**hdata).heditrecord = heditrecord;
  81.     
  82.     app.appdata = (Handle) hdata;
  83.         
  84.     return (true);
  85.     
  86.     error:
  87.     
  88.     disposehandle ((Handle) hdata);
  89.     
  90.     editdispose (heditrecord);
  91.     
  92.     return (false);
  93.     } /*newrecord*/
  94.  
  95.  
  96. static boolean disposerecord (void) {
  97.     
  98.     hdldata hd = (hdldata) app.appdata;
  99.  
  100.     editdispose ((**hd).heditrecord);
  101.     
  102.     disposehandle ((Handle) hd);
  103.         
  104.     return (true);
  105.     } /*disposerecord*/
  106.     
  107.     
  108. static boolean activate (boolean flactive) {
  109.     
  110.     hdldata hd = (hdldata) app.appdata;
  111.     
  112.     (**hd).flactive = flactive;
  113.     
  114.     editactivate ((**hd).heditrecord, flactive);
  115.     
  116.     return (true);
  117.     } /*activate*/
  118.     
  119.     
  120. static boolean update (void) {
  121.     
  122.     hdldata hd = (hdldata) app.appdata;    
  123.  
  124.     editupdate ((**hd).heditrecord);
  125.     
  126.     return (true);
  127.     } /*update*/
  128.  
  129.  
  130. static boolean keystroke (void) { 
  131.     
  132.     hdldata hd = (hdldata) app.appdata;    
  133.             
  134.     editkeystroke (keyboardstatus.chkb, (**hd).heditrecord);
  135.     
  136.     madechanges ();
  137.     
  138.     return (true);
  139.     } /*keystroke*/
  140.     
  141.  
  142. static boolean mousedown (void) {
  143.     
  144.     hdldata hd = (hdldata) app.appdata;    
  145.         
  146.     editclick (mousestatus.localpt, keyboardstatus.flshiftkey, (**hd).heditrecord);
  147.     
  148.     return (true);
  149.     } /*mousedown*/
  150.     
  151.  
  152. static boolean packrecord (Handle *hpacked) {
  153.     
  154.     hdldata hd = (hdldata) app.appdata;    
  155.         
  156.     return (editgettexthandlecopy ((**hd).heditrecord, hpacked));
  157.     } /*packrecord*/
  158.     
  159.     
  160. static boolean unpackrecord (Handle hpacked) {
  161.     
  162.     hdldata hd = (hdldata) app.appdata;    
  163.     hdlappwindow ha = app.appwindow;
  164.         
  165.     if (!editsettexthandle ((**hd).heditrecord, hpacked, false))
  166.         return (false);
  167.         
  168.     editsetrect ((**hd).heditrecord, (**ha).contentrect);
  169.         
  170.     editscroll (
  171.         (**hd).heditrecord, 
  172.         
  173.         -getscrollbarcurrent ((**ha).horizbar), 
  174.         
  175.         -getscrollbarcurrent ((**ha).vertbar));
  176.     
  177.     return (true);
  178.     } /*unpackrecord*/
  179.     
  180.     
  181. static boolean getoptions (Handle *hoptions) {
  182.     
  183.     hdldata hd = (hdldata) app.appdata;    
  184.     tyoptionsrecord x;
  185.     
  186.     clearbytes (&x, longsizeof (x));
  187.     
  188.     editgetselection ((**hd).heditrecord, &x.selstart, &x.selend);
  189.     
  190.     return (newfilledhandle (&x, longsizeof (x), hoptions));
  191.     } /*getoptions*/
  192.     
  193.     
  194. static boolean putoptions (Handle hoptions) {
  195.     
  196.     hdldata hd = (hdldata) app.appdata;    
  197.     tyoptionsrecord x;
  198.     
  199.     moveleft (*hoptions, &x, longsizeof (x));
  200.     
  201.     editsetselection ((**hd).heditrecord, x.selstart, x.selend);
  202.     
  203.     return (true);
  204.     } /*putoptions*/
  205.     
  206.     
  207. static boolean pastetext (Handle htext) {
  208.     
  209.     hdldata hd = (hdldata) app.appdata;    
  210.     
  211.     madechanges ();
  212.         
  213.     return (editreplacehandle (htext, (**hd).heditrecord));
  214.     } /*pastetext*/
  215.     
  216.     
  217. static boolean copytext (Handle *htext) {
  218.     
  219.     hdldata hd = (hdldata) app.appdata;    
  220.         
  221.     return (editgetselectedtexthandle ((**hd).heditrecord, htext));
  222.     } /*copytext*/
  223.     
  224.  
  225. static boolean deleteselectedtext (void) {
  226.     
  227.     hdldata hd = (hdldata) app.appdata;    
  228.     bigstring bs;
  229.     
  230.     setstringlength (bs, 0);
  231.     
  232.     editreplace (bs, (**hd).heditrecord);
  233.     
  234.     madechanges ();
  235.     
  236.     return (true);
  237.     } /*deleteselectedtext*/
  238.     
  239.     
  240. static boolean windowresize (void) {
  241.     
  242.     hdldata hd = (hdldata) app.appdata;    
  243.     
  244.     editsetrect ((**hd).heditrecord, (**app.appwindow).contentrect);
  245.     
  246.     return (true);
  247.     } /*windowresize*/
  248.     
  249.     
  250. static boolean scrollto (void) {
  251.     
  252.     hdldata hd = (hdldata) app.appdata;    
  253.     hdlappwindow ha = app.appwindow;
  254.     
  255.     editscrollto (
  256.         (**hd).heditrecord, 
  257.         
  258.         getscrollbarcurrent ((**ha).horizbar), 
  259.         
  260.         getscrollbarcurrent ((**ha).vertbar));
  261.         
  262.     return (true);
  263.     } /*scrollto*/
  264.     
  265.     
  266. static boolean lowercaseverb (void) {
  267.  
  268.     hdldata hd = (hdldata) app.appdata;    
  269.     Handle htext;
  270.     short ctchars, i;
  271.     
  272.     if (!settargetglobals ()) /*this verb requires an open window*/
  273.         return (false);
  274.         
  275.     editgettexthandle ((**hd).heditrecord, &htext);
  276.     
  277.     ctchars = GetHandleSize (htext);
  278.     
  279.     for (i = 0; i < ctchars; i++) {
  280.         
  281.         char ch = (*htext) [i];
  282.         
  283.         if ((ch >= 'A') && (ch <= 'Z'))
  284.             (*htext) [i] = (char) ch + 32;
  285.         } /*for*/
  286.         
  287.     editupdate ((**hd).heditrecord);
  288.     
  289.     madechanges ();
  290.     
  291.     IACreturnboolean (true);
  292.     
  293.     return (true);
  294.     } /*lowercaseverb*/
  295.     
  296.     
  297. static boolean uppercaseverb (void) {
  298.  
  299.     hdldata hd = (hdldata) app.appdata;    
  300.     Handle htext;
  301.     short ctchars, i;
  302.     
  303.     if (!settargetglobals ()) /*this verb requires an open window*/
  304.         return (false);
  305.         
  306.     editgettexthandle ((**hd).heditrecord, &htext);
  307.     
  308.     ctchars = GetHandleSize (htext);
  309.     
  310.     for (i = 0; i < ctchars; i++) {
  311.         
  312.         char ch = (*htext) [i];
  313.         
  314.         if ((ch >= 'a') && (ch <= 'z'))
  315.             (*htext) [i] = (char) ch - 32;
  316.         } /*for*/
  317.         
  318.     editupdate ((**hd).heditrecord);
  319.     
  320.     madechanges ();
  321.     
  322.     IACreturnboolean (true);
  323.     
  324.     return (true);
  325.     } /*uppercaseverb*/
  326.     
  327.     
  328. static boolean iacmessage (void) {
  329.     
  330.     switch (IACgetverbtoken ()) {
  331.         
  332.         case lowercasetoken:
  333.             lowercaseverb (); break;
  334.             
  335.         case uppercasetoken:
  336.             uppercaseverb (); break;
  337.         
  338.         } /*switch*/
  339.         
  340.     return (true);
  341.     } /*iacmessage*/
  342.     
  343.     
  344. static boolean getpicture (PicHandle *hpicture) {
  345.     
  346.     hdldata hd = (hdldata) app.appdata;    
  347.     Rect r;
  348.     
  349.     *hpicture = nil; /*return value if error*/
  350.     
  351.     if (hd == nil) /*no data, it can't be our window*/
  352.         return (false);
  353.     
  354.     r.top = 0;
  355.     
  356.     r.left = 0;
  357.     
  358.     editgetbuffersize ((**hd).heditrecord, &r.bottom, &r.right);
  359.     
  360.     *hpicture = OpenPicture (&r);
  361.     
  362.     ClipRect (&r);
  363.     
  364.     editupdateport ((**hd).heditrecord, r, qd.thePort);
  365.     
  366.     ClosePicture ();
  367.     
  368.     return (true);
  369.     } /*getpicture*/
  370.     
  371.     
  372. static boolean pagesetup (void) {
  373.     
  374.     return (true);
  375.     } /*pagesetup*/
  376.  
  377.  
  378. static boolean openprint (void) {
  379.     
  380.     hdldata hd = (hdldata) app.appdata;    
  381.     Rect rpict, rpaper;
  382.     PicHandle hpict;
  383.     short ctpages, vpictpixels;
  384.     
  385.     getpicture (&hpict);
  386.     
  387.     (**hd).macpicture = hpict;
  388.     
  389.     rpict = (**hpict).picFrame;
  390.     
  391.     rpaper = app.printinfo.paperrect;
  392.     
  393.     vpictpixels = rpict.bottom - rpict.top;
  394.     
  395.     ctpages = vpictpixels / app.printinfo.vpagepixels;
  396.     
  397.     if (vpictpixels > (ctpages * app.printinfo.vpagepixels))
  398.         ctpages++;
  399.     
  400.     app.printinfo.ctpages = ctpages;
  401.     
  402.     return (true);
  403.     } /*openprint*/
  404.  
  405.  
  406. static boolean printpage (short pagenumber) {
  407.     
  408.     hdldata hd = (hdldata) app.appdata;    
  409.     PicHandle macpicture = (**hd).macpicture;
  410.     short hoffset, voffset;
  411.     Rect rpict, rpaper;
  412.     
  413.     rpict = (**macpicture).picFrame;
  414.     
  415.     rpaper = app.printinfo.paperrect;
  416.     
  417.     hoffset = rpaper.left - rpict.left;
  418.     
  419.     voffset = (rpaper.top - rpict.top) - ((pagenumber - 1) * app.printinfo.vpagepixels) - 1;
  420.     
  421.     SetOrigin (-hoffset, -voffset);
  422.     
  423.     ClipRect (&(*qd.thePort).portRect);
  424.     
  425.     DrawPicture (macpicture, &rpict);
  426.     
  427.     return (true);
  428.     } /*printpage*/
  429.     
  430.     
  431. static boolean closeprint (void) {
  432.     
  433.     hdldata hd = (hdldata) app.appdata;    
  434.     PicHandle hpict = (**hd).macpicture;
  435.     
  436.     if (hpict != nil) {
  437.         
  438.         KillPicture (hpict);
  439.         
  440.         (**hd).macpicture = nil;
  441.         }
  442.     
  443.     return (true);
  444.     } /*closeprint*/
  445.  
  446.  
  447. static boolean haveselection (void) {
  448.     
  449.     hdldata hd = (hdldata) app.appdata;    
  450.     
  451.     return (edithaveselection ((**hd).heditrecord));
  452.     } /*haveselection*/
  453.     
  454.     
  455. static boolean selectall (void) {
  456.     
  457.     hdldata hd = (hdldata) app.appdata;    
  458.     
  459.     editselectall ((**hd).heditrecord);
  460.         
  461.     return (true);
  462.     } /*selectall*/
  463.     
  464.  
  465. static boolean setselectioninfo (void) {
  466.  
  467.     /*
  468.     9/3/92 DW: added code to set x.fontnum, x.fontsize.
  469.     */
  470.     
  471.     hdlappwindow ha = app.appwindow;
  472.     tyselectioninfo x = (**ha).selectioninfo;
  473.     
  474.     x.flcansetfont = true;
  475.     
  476.     x.flcansetsize = true;
  477.     
  478.     x.flcansetstyle = false;
  479.     
  480.     x.flcansetjust = false;    
  481.     
  482.     x.fldirty = false;
  483.     
  484.     x.fontnum = (**ha).defaultfont;
  485.     
  486.     x.fontsize = (**ha).defaultsize;
  487.     
  488.     (**ha).selectioninfo = x;
  489.     
  490.     return (true);
  491.     } /*setselectioninfo*/
  492.     
  493.         
  494. static boolean setfont (void) {
  495.     
  496.     hdldata hd = (hdldata) app.appdata;    
  497.     hdlappwindow ha = app.appwindow;
  498.     tyselectioninfo x = (**ha).selectioninfo;
  499.     
  500.     if (x.fontnum != -1)
  501.         (**ha).defaultfont = x.fontnum;
  502.     
  503.     if (x.fontsize != -1)
  504.         (**ha).defaultsize = x.fontsize;
  505.     
  506.     editsetfont ((**hd).heditrecord, (**ha).defaultfont, (**ha).defaultsize);
  507.         
  508.     invalappwindow (ha, true);
  509.         
  510.     return (true);
  511.     } /*setfont*/
  512.     
  513.     
  514. static boolean getcontentsize (void) {
  515.     
  516.     hdldata hd = (hdldata) app.appdata;    
  517.     hdlappwindow ha = app.appwindow;
  518.     short height, width;
  519.     
  520.     pushstyle ((**ha).defaultfont, (**ha).defaultsize, (**ha).defaultstyle);
  521.     
  522.     editgetbuffersize ((**hd).heditrecord, &height, &width);
  523.     
  524.     popstyle ();
  525.     
  526.     (**ha).zoomheight = height;
  527.     
  528.     (**ha).zoomwidth = width;
  529.     
  530.     return (true);
  531.     } /*getcontentsize*/
  532.     
  533.     
  534. static boolean idle (void) {
  535.     
  536.     /*
  537.     this code is guaranteed to be running when minapp is the current context,
  538.     not from a fast event handler. so we process display oriented tasks 
  539.     generated by fast event handlers in this callback.
  540.     */
  541.     
  542.     hdlappwindow ha = app.appwindow;
  543.     hdldata hd = (hdldata) app.appdata;
  544.     Point pt = mousestatus.localpt;
  545.     hdleditrecord he;
  546.     
  547.     if (hd == nil)
  548.         return (true);
  549.         
  550.     he = (**hd).heditrecord;
  551.     
  552.     if ((**he).flscrolled) {
  553.     
  554.         setscrollbarcurrent ((**ha).vertbar, (**he).vertcurrent);
  555.         
  556.         setscrollbarcurrent ((**ha).horizbar, (**he).horizcurrent);
  557.         
  558.         (**ha).flresetscrollbars = true; /*force max and min to be recalculated*/
  559.         
  560.         (**he).flscrolled = false; /*consume it*/
  561.         }
  562.     
  563.     if (PtInRect (pt, &(**ha).contentrect))
  564.         setcursortype (cursorisibeam);
  565.     else
  566.         setcursortype (cursorisarrow);
  567.         
  568.     editidle (he);
  569.     
  570.     return (true);
  571.     } /*idle*/
  572.     
  573.  
  574. void main (void) {
  575.  
  576.     clearbytes (&app, longsizeof (app)); /*init all fields to 0*/
  577.     
  578.     app.creator = 'MINA'; 
  579.     
  580.     app.filetype = 'TEXT';
  581.     
  582.     app.haswindowmenu = true; /*3.0*/
  583.     
  584.     app.usetempmemory = true; /*3.0*/
  585.     
  586.     app.resizeable = true;
  587.     
  588.     app.eraseonresize = true;
  589.     
  590.     app.horizscroll = true;
  591.     
  592.     app.vertscroll = true;
  593.     
  594.     app.usecolor = false;
  595.     
  596.     app.defaultfont = monaco;
  597.     
  598.     app.defaultsize = 9;
  599.     
  600.     app.defaultstyle = 0;
  601.     
  602.     app.newrecordcallback = &newrecord;
  603.     
  604.     app.disposerecordcallback = &disposerecord;
  605.     
  606.     app.idlecallback = &idle;
  607.     
  608.     app.activatecallback = (tyappbooleancallback) &activate;
  609.     
  610.     app.updatecallback = &update;
  611.     
  612.     app.iacmessagecallback = &iacmessage;
  613.     
  614.     app.packcallback = &packrecord;
  615.     
  616.     app.gettextcallback = ©text;
  617.     
  618.     app.puttextcallback = &pastetext;
  619.     
  620.     app.clearcallback = &deleteselectedtext;
  621.     
  622.     app.unpackcallback = &unpackrecord;
  623.     
  624.     app.keystrokecallback = &keystroke;
  625.     
  626.     app.mousecallback = &mousedown;
  627.     
  628.     app.windowresizecallback = &windowresize;
  629.     
  630.     app.scrolltocallback = &scrollto;
  631.     
  632.     app.openprintcallback = &openprint;
  633.     
  634.     app.pagesetupcallback = &pagesetup;
  635.     
  636.     app.printpagecallback = (tyappshortcallback) &printpage;
  637.     
  638.     app.closeprintcallback = &closeprint;
  639.     
  640.     app.setselectioninfocallback = &setselectioninfo;
  641.     
  642.     app.haveselectioncallback = &haveselection;
  643.     
  644.     app.selectallcallback = &selectall;
  645.     
  646.     app.setfontcallback = &setfont;
  647.     
  648.     app.setsizecallback = &setfont;
  649.     
  650.     app.getcontentsizecallback = &getcontentsize;
  651.     
  652.     app.getoptionscallback = &getoptions;
  653.     
  654.     app.putoptionscallback = &putoptions;
  655.     
  656.     runapplet ();
  657.     } /*main*/
  658.     
  659.  
  660.